package com.orangelife.mobile.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;

/**
 *
 * Created by HeYuCan on 2016/9/8.
 */
public class PopupDialog extends Dialog {

    private Window window = null;
    //新浪微博按钮，朋友圈按钮，微信好友按钮，QQ按钮
    private ImageButton ibWeibo,ibPengyouquan,ibWeixin,ibQQ;
    //取消按钮
    private TextView tvCancel;
    private View mMenuView;
    private Context context;
    private int height;


    public PopupDialog(Context context) {
        super(context, R.style.PopupDialogStyle);
        //super(context);
        this.context=context;

    }

    public void showDialog(int layoutResID,View.OnClickListener itemsOnClick) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_share, null);
        height=view.getHeight();
        setContentView(view);
        windowDeploy();
        init(layoutResID,itemsOnClick);
        show();
        //设置触摸对话框意外的地方取消对话框
        setCanceledOnTouchOutside(true);
    }

    /**
     * 目前只用于分享的弹出框 待扩展
     * @param layoutResID  被写死了 R.layout.dialog_share
     * @param itemsOnClick
     */
    private void init(int layoutResID,View.OnClickListener itemsOnClick) {
//        LayoutInflater inflater = (LayoutInflater) context
//                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        mMenuView = inflater.inflate(layoutResID, null);
        ibPengyouquan = (ImageButton) window.findViewById(R.id.ib_pengyouquan);
        ibWeibo = (ImageButton) window.findViewById(R.id.ib_weibo);
        ibWeixin = (ImageButton) window.findViewById(R.id.ib_weixin);
        ibQQ = (ImageButton) window.findViewById(R.id.ib_qq);
        tvCancel = (TextView) window.findViewById(R.id.tv_cancel);
        mMenuView=window.findViewById(layoutResID);

        //取消按钮
        tvCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //销毁弹出框
                dismiss();
                DataTrack.clickTrack("btnCancel",null);
            }
        });
        //设置按钮监听
        ibPengyouquan.setOnClickListener(itemsOnClick);
        ibWeibo.setOnClickListener(itemsOnClick);
        ibWeixin.setOnClickListener(itemsOnClick);
        ibQQ.setOnClickListener(itemsOnClick);


        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
//        mMenuView.setOnTouchListener(new View.OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//
//                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
//                int y=(int) event.getY();
//                if(event.getAction()==MotionEvent.ACTION_UP){
//                    if(y<height){
//                        dismiss();
//                    }
//                }
//                return true;
//            }
//        });
    }

    //设置窗口显示
    public void windowDeploy() {
        window = getWindow(); //得到对话框
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        //window.setBackgroundDrawableResource(R.color.translucent_background); //设置对话框背景为半透明
        //window.setBackgroundDrawableResource(R.color.white);

        WindowManager.LayoutParams wl = window.getAttributes();
        //根据x，y坐标设置窗口需要显示的位置
//        wl.x = x; //x小于0左移，大于0右移
//        wl.y = y; //y小于0上移，大于0下移
//            wl.alpha = 0.6f; //设置透明度
        wl.width = WindowManager.LayoutParams.MATCH_PARENT;
//        wl.alpha=1;
//        wl.buttonBrightness=0.3f;
//        wl.flags= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        wl.gravity = Gravity.BOTTOM; //设置重力
        window.setAttributes(wl);

    }
}
